package com.zhijian.wharf.job;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.zhijian.wharf.entity.po.DatabaseDelete;
import com.zhijian.wharf.entity.vo.request.EnvMonitorSearchVo;
import com.zhijian.wharf.service.DatabaseDeleteService;
import com.zhijian.wharf.service.EnvMonitorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @author HCG
 * @version 1.0
 * @date 2024-08-16
 */
@Service
public class MysqlDeleteJob {

    @Autowired
    private EnvMonitorService envMonitorService;

    @Autowired
    private DatabaseDeleteService databaseDeleteService;

    /**
     * 一天执行一次
     */
    @Scheduled(cron = "${job.point.cron:0 4 1 * * * }")
    public void handle() {
        DatabaseDelete databaseDelete = new DatabaseDelete();
        Date now = new Date();
        DateTime dateTime = DateUtil.offsetDay(now, -15);
        //删除
        int i = envMonitorService.deleteByCondition(EnvMonitorSearchVo.builder()
                .deleteTime(DateUtil.formatDateTime(dateTime))
                .build());
        databaseDelete.setTableName("env_monitor");
        databaseDelete.setDeleteCount((long) i);
        databaseDeleteService.insert(databaseDelete);
    }

}
